#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

void func(int num, mutex &lock, mutex &b_lock) {
    for (int i = 0; i < 10; i++) {
        lock.lock();
        cout << num << ", ";
        b_lock.unlock();
    }
}

int main(void)
{
    cout << "# 3 thread that print 1,2,3 output by sort 1,2,3" << endl;

    mutex lock1, lock2, lock3;
    lock2.lock();
    lock3.lock();
    thread t1 = thread(func, 1, ref(lock1), ref(lock2));
    thread t2 = thread(func, 2, ref(lock2), ref(lock3));
    thread t3 = thread(func, 3, ref(lock3), ref(lock1));

    t1.join();
    t2.join();
    t3.join();
    cout << endl;

    return 0;
}